package com.big.tree.domain;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.big.tree.utils.ConvertUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Description
 * Author ayt  on
 */
public class Provider {

	private static final long serialVersionUID = 5981342400350878171L;

	private String id;
	private String service;/* The name of the service provided by the provider */

	private String url; /* Provider's address for service */

	private String parameters; /* Provider provides service parameters */

	private String address; /* Provider address */

	private String registry;/* The provider's registry address */

	private boolean dynamic;          /* provider was registered dynamically */

	private boolean enabled;          /* provider enabled or not */

	private int weight;          /* provider weight */

	private String application; /* application name */

	private String username;      /* operator */

	private Date expired;   /* time to expire */

	private long alived;    /* time to live in milliseconds */

	private Override override;

	private List<Override> overrides;

	public Provider() {
	}


	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getParameters() {
		return parameters;
	}

	public void setParameters(String parameters) {
		this.parameters = parameters;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getRegistry() {
		return registry;
	}

	public void setRegistry(String registry) {
		this.registry = registry;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getApplication() {
		return application;
	}

	public void setApplication(String application) {
		this.application = application;
	}

	public boolean isDynamic() {
		return dynamic;
	}

	public void setDynamic(boolean dynamic) {
		this.dynamic = dynamic;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}


	public Date getExpired() {
		return expired;
	}


	public void setExpired(Date expired) {
		this.expired = expired;
	}

	public long getAlived() {
		return alived;
	}

	public void setAlived(long aliveSeconds) {
		this.alived = aliveSeconds;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public Override getOverride() {
		return override;
	}

	public void setOverride(Override override) {
		this.override = override;
	}

	public List<Override> getOverrides() {
		return overrides;
	}

	public void setOverrides(List<Override> overrides) {
		this.overrides = overrides;
	}

	public URL toUrl() {
		Map<String, String> serviceName2Map = ConvertUtil.serviceName2Map(getService());
        /*if(!serviceName2Map.containsKey(Constants.INTERFACE_KEY)) {
            throw new IllegalArgumentException("No interface info");
        }
        if(!serviceName2Map.containsKey(Constants.VERSION_KEY)) {
            throw new IllegalArgumentException("No version info");
        }*/

		String u = getUrl();
		URL url = URL.valueOf(u + "?" + getParameters());

		url = url.addParameters(serviceName2Map);

		boolean dynamic = isDynamic();
		if (!dynamic) {
			url = url.addParameter(Constants.DYNAMIC_KEY, false);
		}
		boolean enabled = isEnabled();
		if (enabled != url.getParameter("enabled", true)) {
			if (enabled) {
				url = url.removeParameter("enabled");
			} else {
				url = url.addParameter("enabled", false);
			}
		}
		return url;
	}
}
